﻿using System;
using System.Text;
using System.Data;
using System.Drawing;
using System.Windows.Forms;
using System.ComponentModel;
using System.Collections.Generic;
using GenaralLibs;
using Lists;

namespace QLKDMain.HoSo
{
    //Design by Pongsakorn Poosankam
    public partial class FrmXuLyViPham : FrmOKCancel
    {
        HoSoDangKy _HoSoDangKy = null;
        DSHoSo _DSHoSo = new DSHoSo();
        public FrmXuLyViPham(HoSoDangKy hoSoDangKy)
        {
            InitializeComponent();
            _HoSoDangKy = hoSoDangKy;
        }

        private void FrmXuLyViPham_Load(object sender, EventArgs e)
        {
            DevExpress.XtraEditors.Controls.LookUpColumnInfo tenMucXuPhatInfo = new DevExpress.XtraEditors.Controls.LookUpColumnInfo("TenMuc", "Tên mục", 20);
            DevExpress.XtraEditors.Controls.LookUpColumnInfo mucXuPhatInfo = new DevExpress.XtraEditors.Controls.LookUpColumnInfo("SoTien", "Số tiền phạt", 20);
            DevExpress.XtraEditors.Repository.RepositoryItemLookUpEdit repLookupMucXuPhat = new DevExpress.XtraEditors.Repository.RepositoryItemLookUpEdit();
            repLookupMucXuPhat.Columns.Add(tenMucXuPhatInfo);
            repLookupMucXuPhat.Columns.Add(mucXuPhatInfo);
            repLookupMucXuPhat.ValueMember = "ID";
            repLookupMucXuPhat.DisplayMember = "SoTien";
            repLookupMucXuPhat.DataSource = DanhMucMucXuPhat.Instance.DataView;

            colPhatTien.ColumnEdit = repLookupMucXuPhat;
            colPhatTien.FilterMode = DevExpress.XtraGrid.ColumnFilterMode.DisplayText;

            if (_HoSoDangKy != null && _HoSoDangKy.ID > 0) gridControlDs.DataSource = DanhMucXuLyViPham.Instance.GetByHoSoID(_HoSoDangKy.ID).DataView.ToTable();
            else
            {
                MessageBox.Show("Hồ sơ đăng ký mới được tạo.\nChưa thể điền thông tin vi phạm");
                this.Close();
            }
            (gridControlDs.DataSource as DataTable).AcceptChanges();
            #region cmbNganhNghe
            cmbNganhNghe.DataSource = DanhMucNganhNghe.Instance.DataView;
            cmbNganhNghe.DisplayColumn = "TenNganhNghe";
            cmbNganhNghe.ValueColumn = "ID";
            cmbNganhNghe.AddVisibleColumn("ID", "Mã");
            cmbNganhNghe.AddVisibleColumn("TenNganhNghe", "Tên ngành nghề");
            #endregion
            //Thong tin chi tiet cho hoso
            txtCMND.Text = _HoSoDangKy.SoCMND;
            txtDienThoai.Text = _HoSoDangKy.DienThoai;
            txtSoGiayPhep.Text = _HoSoDangKy.SoGiayPhepKD;
            txtSoHoSo.Text = _HoSoDangKy.SoHoSo;
            txtTenChuCoSo.Text = _HoSoDangKy.ChuCoSo;
            txtTenCSKD.Text = _HoSoDangKy.TenCoSoKD;
            cmbNganhNghe.EditValue = _HoSoDangKy.NganhNgheID;
            if (_HoSoDangKy.NgayCap is DateTime?) dateNgayCap.EditValue = _HoSoDangKy.NgayCap;
        }

        private void btnOK_Click_1(object sender, EventArgs e)
        {
            try
            {
                if (gridControlDs.DataSource != null && gridControlDs.DataSource is DataTable)
                {
                    foreach (DataRow row in (gridControlDs.DataSource as DataTable).Rows)
                    {
                        //if (row.RowState != DataRowState.Unchanged)
                        //{
                            int id = 0;
                            XuLyViPham xuLyViPham = null;
                            try
                            {
                                id = Convert.ToInt32(row["ID"].ToString());
                                xuLyViPham = DanhMucXuLyViPham.Instance.GetByID(id);
                                if (xuLyViPham == null)
                                {
                                    xuLyViPham = DanhMucXuLyViPham.Instance.NewDoiTuong();
                                    xuLyViPham.HoSoID = _HoSoDangKy.ID;
                                    xuLyViPham.SoTT = Convert.ToInt32(row["SoTT"].ToString());
                                    xuLyViPham.NgayXuLy = Convert.ToDateTime(row["NgayXuLy"].ToString());
                                    xuLyViPham.HanhViiPham = row["HanhViiPham"].ToString();
                                    xuLyViPham.DonViXuLy = row["DonViXuLy"].ToString();
                                    xuLyViPham.MucXuPhatID = Convert.ToInt32(row["MucXuPhatID"].ToString());
                                    xuLyViPham.GhiChu = row["GhiChu"].ToString();
                                    xuLyViPham.InsertToDB();
                                    
                                }
                                else
                                {
                                    xuLyViPham.HoSoID = _HoSoDangKy.ID;
                                    xuLyViPham.SoTT = Convert.ToInt32(row["SoTT"].ToString());
                                    xuLyViPham.NgayXuLy = Convert.ToDateTime(row["NgayXuLy"].ToString());
                                    xuLyViPham.HanhViiPham = row["HanhViiPham"].ToString();
                                    xuLyViPham.DonViXuLy = row["DonViXuLy"].ToString();
                                    xuLyViPham.MucXuPhatID = Convert.ToInt32(row["MucXuPhatID"].ToString());
                                    xuLyViPham.GhiChu = row["GhiChu"].ToString();
                                    xuLyViPham.UpdateToDB();
                                }
                            }
                            catch (Exception ex)
                            {
                                xuLyViPham = DanhMucXuLyViPham.Instance.NewDoiTuong();
                                xuLyViPham.HoSoID = _HoSoDangKy.ID;
                                xuLyViPham.SoTT = Convert.ToInt32(row["SoTT"].ToString());
                                xuLyViPham.NgayXuLy = Convert.ToDateTime(row["NgayXuLy"].ToString());
                                xuLyViPham.HanhViiPham = row["HanhViiPham"].ToString();
                                xuLyViPham.DonViXuLy = row["DonViXuLy"].ToString();
                                xuLyViPham.MucXuPhatID = Convert.ToInt32(row["MucXuPhatID"].ToString());
                                xuLyViPham.GhiChu = row["GhiChu"].ToString();
                                xuLyViPham.InsertToDB();
                                
                            }
                        //}
                    }
                }

                if (_HoSoDangKy != null && _HoSoDangKy.ID > 0)
                {
                    gridControlDs.DataSource = DanhMucXuLyViPham.Instance.GetByHoSoID(_HoSoDangKy.ID).DataView.ToTable();
                    foreach (DataRow row in (gridControlDs.DataSource as DataTable).Rows)
                    {
                        int id = Convert.ToInt32(row["ID"].ToString());
                        XuLyViPham xuLyViPham = DanhMucXuLyViPham.Instance.GetByID(id);
                        //lblShowViPham.Text = lblShowViPham.Text + string.Format("Lần {0}: Nội dung vi phạm = {1}, Mức xử phạt = {2}          \n",
                        //                                                                    xuLyViPham.SoTT, xuLyViPham.HanhViiPham, xuLyViPham.DMMucXuPhat.SoTien);
                    }
                }

                this.DialogResult = DialogResult.OK;
                this.Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
    }
}
